package com.example.blog_system_ssm.controller;

import com.example.blog_system_ssm.mapper.BlogMapper;
import com.example.blog_system_ssm.mapper.UserMapper;
import com.example.blog_system_ssm.model.Blog;
import com.example.blog_system_ssm.model.User;
import com.example.blog_system_ssm.tools.Constant;
import com.example.blog_system_ssm.tools.ResponseBodyMessage;
import com.sun.org.apache.xpath.internal.operations.Bool;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@RequestMapping("/blog")
@RestController
public class BlogController {

    @Resource
    private UserMapper userMapper;

    @Resource
    private BlogMapper blogMapper;

    @RequestMapping("/findblog")
    public ResponseBodyMessage<List<Blog>>  getBlog(@RequestParam(required = false)String blogId, HttpServletRequest request){

        //1. 检查是否登录
        HttpSession session=request.getSession(false);
        if (session==null || session.getAttribute(Constant.USERINFO_SESSION_KEY)==null){
            System.out.println("当前用户未登录");
            return new ResponseBodyMessage<>(-1,"请先登录再操作",null);
        }
        User user=(User) session.getAttribute(Constant.USERINFO_SESSION_KEY);
        int userId=user.getUserId();
        System.out.println("userId:"+userId);

        List<Blog> blogList=null;
        if (blogId==null){
            blogList=blogMapper.selectAllBlog();
        }else {
            int blogIdInt=Integer.parseInt(blogId);
            Blog blog=blogMapper.selectBlogById(blogIdInt);
        }
        if (blogList.size()==0){
            return new ResponseBodyMessage<>(-1,"查无博客",blogList);
        }else {
            return new ResponseBodyMessage<>(0,"查询成功，博客数目是"+blogList.size(),blogList);
        }
    }

    @RequestMapping("findblogbyblogid")
    public ResponseBodyMessage<Blog> findBlogByBlogId(@RequestParam String blogId){
        int blodIId=Integer.parseInt(blogId);
        Blog blog=blogMapper.selectBlogById(blodIId);
        if (blog==null){
            return new ResponseBodyMessage<>(-1,"查无此博客",null);
        }else {
            return new ResponseBodyMessage<>(0,"查询指定id博客成功",blog);
        }
    }

    @RequestMapping("/delete")
    public ResponseBodyMessage<Boolean> deleteBlogById(@RequestParam String blogId){
        int id=Integer.parseInt(blogId);
        int row=blogMapper.deleteBlogById(id);
        if (row==0){
            System.out.println("该博客不存在");
            return new ResponseBodyMessage<>(-1,"指定博客不存在",false);
        }else {
            return new ResponseBodyMessage<>(0,"删除成功",true);
        }

    }

    @RequestMapping("/insert")
    public ResponseBodyMessage<Boolean> inserBlog(@RequestParam String title,@RequestParam String content,HttpServletRequest request){
        //1. 检查是否登录
        HttpSession session=request.getSession(false);
        if (session==null || session.getAttribute(Constant.USERINFO_SESSION_KEY)==null){
            System.out.println("当前用户未登录");
            return new ResponseBodyMessage<>(-1,"请先登录再操作",null);
        }
        User user=(User) session.getAttribute(Constant.USERINFO_SESSION_KEY);
        int userId=user.getUserId();
        System.out.println("userId:"+userId);

        Blog blog=new Blog();
        blog.setTitle(title);
        blog.setContent(content);
        blog.setUserId(userId);

        int ret=blogMapper.insertBlog(blog);

        if (ret==1){
            return new ResponseBodyMessage<>(0,"博客新增成功",true);
        }else {
            return new ResponseBodyMessage<>(-1,"博客新增失败",false);
        }
    }
}
